package com.cloudfun.campusshare.device.protocol.decoder;

import com.cloudfun.campusshare.device.protocol.response.BxyState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * 波轮洗衣机
 * Created by Huqin on 2020/1/15
 */
public class BxyStateMessageDecoder extends ByteToMessageDecoder<BxyState> {

    private List<FieldDecoder<BxyState>> fieldDecoderList;

    public BxyStateMessageDecoder() {
        fieldDecoderList = new ArrayList<>();
        // 洗衣程序 washprog
        fieldDecoderList.add(new EnumFieldDecoder<>(0, 1, BxyState::setLaundryCycleCW, this.washTypeMap()));
        // 程序运行的阶段 stage
        fieldDecoderList.add(new EnumFieldDecoder<>(4, 1, BxyState::setCyclePhaseCW, this.stageMap()));
        // 状态 state1
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(5, 1, this.stage1Map()));
        // 当前剩余/预约时间的小时部分 remainprectime Hour
        fieldDecoderList.add(new DigitFieldDecoder<>(7, 1, BxyState::setRemainingTimeHH));
        // 当前剩余/预约时间的分钟部分 remainprectime Minute
        fieldDecoderList.add(new DigitFieldDecoder<>(8, 1, BxyState::setRemainingTimeMM));
        // 提示信息 message
        fieldDecoderList.add(new EnumFieldDecoder<>(9, 1, BxyState::setMessage, this.messageMap()));
    }

    @Override
    protected List<FieldDecoder<BxyState>> fieldDecoderList() {
        return fieldDecoderList;
    }

    @Override
    protected BxyState prepare() {
        return new BxyState();
    }

    private Map<String, String> messageMap() {
        Map<String, String> messageMap = new HashMap<>();
        messageMap.put("01", "衣服已洗完");
        return messageMap;
    }

    private Map<Integer, BiConsumer<BxyState, Boolean>> stage1Map() {
        Map<Integer, BiConsumer<BxyState, Boolean>> stage1Map = new HashMap<>();
        stage1Map.put(5,  BxyState::setDoorClosed);
        stage1Map.put(6,  BxyState::setLaundryCycleStatus);
        stage1Map.put(7, BxyState::setStarted);
        return stage1Map;
    }

    private Map<String, Integer> stageMap() {
        Map<String, Integer> stageMap = new HashMap<>();
        stageMap.put("00", 0);//待机
        //stageMap.put("01", "称重");
        stageMap.put("03", 1);//洗涤进水
        stageMap.put("05", 2);//洗涤
        stageMap.put("06", 3);//洗涤排水
        stageMap.put("07", 4);//中间脱水
        stageMap.put("08", 5);//漂洗进水
        stageMap.put("09", 6);//漂洗
        stageMap.put("0A", 7);//漂洗排水
        stageMap.put("0B", 8);//最终脱水
        stageMap.put("0E", 9);//结束
        return stageMap;
    }

    private Map<String, Integer> washTypeMap() {
        Map<String, Integer> washTypeMap = new HashMap<>();
        washTypeMap.put("23", 1);//1标准
        washTypeMap.put("0F", 2);//2快速
        washTypeMap.put("11", 3);//3大物
        washTypeMap.put("1C", 4);//4单脱
        washTypeMap.put("0D", 5);//5桶自洁
        return washTypeMap;
    }

}
